package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    //添加方法
    @Override
    public void addsave(SetmealDto setmealDto) {
        //因为套餐是一定有的
        setmealMapper.insert(setmealDto);
        //用套餐对象拿到所有菜品集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //因为有可能没传入对象
        if(setmealDishes!=null&&setmealDishes.size()>0){
            //通过传来的综合对象得到套餐的id
            Long setmealDtoId = setmealDto.getId();
            //遍历套餐集合得到每一个菜品
            for (SetmealDish setmealDish : setmealDishes) {
                //把套餐的id传入菜品对象
                setmealDish.setSetmealId(setmealDtoId);
                //把菜品一个一个放入数据库
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
    //删除套餐
    @Override
    public int deletes(List<Long> ids) {
        //遍历套餐id
        for (Long id : ids) {
            //调用删除方法 条件是状态为0并且id为传过来的id
            LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
           qw.eq(Setmeal::getStatus,"0").in(Setmeal::getId,id);
            return setmealMapper.delete(qw);
        }
        return 0;
    }
   //修改状态
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //遍历套餐的id
        for (Long id : ids) {
            //创建套餐的对象
            Setmeal setmeal=new Setmeal();
            //修改套餐的状态
            setmeal.setStatus(status);
            //设置套餐对象id
            setmeal.setId(id);
           /* QueryWrapper<Setmeal> qw=new QueryWrapper<>();
            qw.lambda().eq(Setmeal::getId,id);*/
            //把修改对象传入套餐的修改方法
             setmealMapper.updateById(setmeal);

        }
    }
   //回显数据
    @Override
    public SetmealDto selectSetmeal(Long id) {
        //套餐mapper根据id拿到回显的套餐对象
        Setmeal setmeal = setmealMapper.selectById(id);
        //创建条件过滤
        QueryWrapper<SetmealDish> qw=new QueryWrapper<>();
        //根据查到对象的id也就是传过来的id
        qw.lambda().eq(SetmealDish::getSetmealId,setmeal.getId());
        //查询所有菜品根据id
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        //创建综合对象
        SetmealDto setmealDto=new SetmealDto();
        //把回显的对象拷入综合对象
        BeanUtils.copyProperties(setmeal,setmealDto);
        //用综合对象的set方法传入查出来的所有菜品集合
        setmealDto.setSetmealDishes(setmealDishes);
        //返回这个综合对象
        return setmealDto;
    }
   //修改数据
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //dto得id是setmeal的id
        Long setmealDtoId = setmealDto.getId();
        //过滤条件
        QueryWrapper<SetmealDish> qw=new QueryWrapper<>();
        //拿到setmealId和SetmealDish的SetmealId进行匹配
        qw.lambda().eq(SetmealDish::getSetmealId,setmealDtoId);
        //先删除这部分数据
        setmealDishMapper.delete(qw);
        //在更改setmeal的数据
        setmealMapper.updateById(setmealDto);
        //setmealDto拿到新写的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //判断一下写没写
        if(setmealDishes!=null&&setmealDishes.size()>0) {
            //因为菜品不止一个所以遍历一下
            for (SetmealDish setmealDish : setmealDishes) {
                //把这个菜品的id放进去
              setmealDish.setSetmealId(setmealDtoId);
              //把菜品更新
              setmealDishMapper.insert(setmealDish);
            }
        }
    }
}
